<?php
require_once 'include/model/Event.php';
require_once 'include/model/PriceCategory.php';
require_once 'include/model/Supplement.php';
//To test this shizzle: dump($_POST, false);

$isPostCorrect = true;
$errorMessage = "Er ging iets bijzonder mis. Stuur ons snel een mailtje!";

//
// check vars
//
// check event
if (isset ($_POST['event_id'])) $event = Event::loadEvent((integer) $_POST['event_id']);
else $isPostCorrect = false;
// check email
if (!($isPostCorrect && isset ($_POST['inschr_em'])))
{
    $errorMessage = "E-mailadres niet ingevuld";
    $isPostCorrect = false;
}
elseif (!preg_match("/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/", $_POST['inschr_em']))
{
    $errorMessage = "E-mailadres is niet correct";
    $isPostCorrect = false;
} else $email = $_POST['inschr_em'];
// check lastname
if (!($isPostCorrect && isset($_POST['inschr_fm'])))
{
    $errorMessage = "Achternaam niet ingevuld";
    $isPostCorrect = false;
} else $registrationName = $_POST['inschr_fm'];

// process entries for main event
for ($i = 0; $i < count($_POST['part_main_fn']); $i++) {
    if(!(isset($_POST['part_main_vn'][$i]) && !empty($_POST['part_main_vn'][$i])
            && isset($_POST['part_main_fn'][$i]) && !empty($_POST['part_main_fn'][$i])
            && isset($_POST["part_soort_main"][$i]) && !empty($_POST['part_soort_main'][$i]))){
        $isPostCorrect = false;
        $errorMessage = "Sommige namen zijn niet ingevuld";
    }
}

// process entries for subevent
if (isset ($_POST['subevent_id'])){
    for ($i = 0; $i < count($_POST['part_main_fn']); $i++) {
        if(!(isset($_POST['part_main_vn'][$i]) && !empty($_POST['part_main_vn'][$i])
                && isset($_POST['part_main_fn'][$i]) && !empty($_POST['part_main_fn'][$i])
                && isset($_POST["part_soort_main"][$i]) && !empty($_POST['part_soort_main'][$i]))){
            $isPostCorrect = false;
            $errorMessage = "Sommige namen zijn niet ingevuld";
        }
    }
}

if($isPostCorrect) {
    // Main variables
    $mainEventId = $_POST['event_id'];
    $mainFirstname = $_POST['part_main_vn'];
    $mainLastname = $_POST['part_main_fn'];
    $mainSort = $_POST['part_soort_main'];
    $mainAmount;

    // Sub variables
    $subEventId = $_POST['subevent_id'];
    $subFirstname = $_POST['part_sub_vn'];
    $subLastname = $_POST['part_sub_fn'];
    $subSort = $_POST['part_soort_sub'];
    $subAmount;

    // Registration variables
    $regName = $_POST['inschr_fm'];
    $regEmail = $_POST['inschr_em'];


    // Main registration
    $mainEvent = Event::loadEvent((integer) $mainEventId);
    $mainReg = $mainEvent->createRegistration();

    for ($i = 0; $i < count($mainLastname); $i++) {
            $entry = $mainReg->newEntry();
            $entry->setFirstname($mainFirstname[$i]);
            $entry->setLastname($mainLastname[$i]);
            //$entry->setLinkedMember(null);  // TODO add this functionality
            $entry->setPriceCategory(PriceCategory::loadPriceCategory($mainEvent, (integer) $mainSort[$i]));

            $mainAmount = count($mainEvent->getSupplements());
            $supps = array();
            for ($j = 1; $j <= $mainAmount; $j++) {
                    $sid = $_POST['part_chb_'.$j];
                    if ($sid[$i] != 0) {
                            $supps[] = Supplement::loadSupplement((integer) $sid[$i]);
                    }
            }
            $entry->setSupplements($supps);
    }
    $mainReg->setEmail($regEmail);
    $mainReg->setResponsibleName($regName);
    $mainReg->save();

    // Sub registration
    $subEvent = Event::loadEvent((integer) $subEventId);
    $subReg = $subEvent->createRegistration();

    for ($i = 0; $i < count($subLastname); $i++) {
            $entry = $subReg->newEntry();
            $entry->setFirstname($subFirstname[$i]);
            $entry->setLastname($subLastname[$i]);
            //$entry->setLinkedMember(null);  // TODO add this functionality
            $entry->setPriceCategory(PriceCategory::loadPriceCategory($subEvent, (integer) $subSort[$i]));

            $subAmount = count($subEvent->getSupplements()) + $mainAmount;
            $supps = array();
            for ($j = $mainAmount+1; $j <= $subAmount; $j++) {
                    $sid = $_POST['part_chb_'.$j];
                    if ($sid[$i] != 0) {
                            $supps[] = Supplement::loadSupplement((integer) $sid[$i]);
                    }
            }
            $entry->setSupplements($supps);
    }
    $subReg->setEmail($regEmail);
    $subReg->setResponsibleName($regName);
    $subReg->save();




    /* Extra part to display the written values, by reading them immediatly from the database */

    echo "<h2>Registraties opgeslaan, hieronder data gelezen uit de databank.</h2>";

    $mreg = Registration::loadRegistration($mainReg->getId());

    // Read main registration from database
    echo "<h4>".$mreg->getEvent()->getName()."</h4>";
    echo "Responsible: ".$mreg->getResponsibleName()." (".$mreg->getEmail().")<br/>";
    $es = $mreg->getEntries();
    echo "<ul>";
    foreach ($es as $e) {
            echo "<li>".$e->getLastname()." ".$e->getFirstname()." (".$e->getPriceCategory().")<br/>";
            $ss = $e->getSupplements();
            foreach ($ss as $s) {
                    echo "[".$s->getName().",".$s->getPrice()."]";
            }
            echo "</li>";
    }
    echo "</ul>";


    $mreg = Registration::loadRegistration($subReg->getId());
    // Read sub registration from database
    echo "<h4>".$mreg->getEvent()->getName()."</h4>";
    $es = $mreg->getEntries();
    echo "<ul>";
    foreach ($es as $e) {
            echo "<li>".$e->getLastname()." ".$e->getFirstname()." (".$e->getPriceCategory().")<br/>";
            $ss = $e->getSupplements();
            foreach ($ss as $s) {
                    echo "[".$s->getName().",".$s->getPrice()."]";
            }
            echo "</li>";
    }
    echo "</ul>";

}
else
{
    // something went wrong
    echo "<h2>$%@&§#! Something went wrong!</h2>";
    echo "<p>$errorMessage</p>";
}